// pages/user/info.js
var app = getApp();
Page({
  data:{
    loginImg:'',
    loginName:'',
    aliasName:'',
    phoneNumber:'',
    loading:false
  },
  onLoad:function(options){
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    that.setData({
      loginImg:wx.getStorageSync('loginInfo').image.absoluteImagePath,
      loginName:wx.getStorageSync('loginInfo').user.loginName,
      aliasName:wx.getStorageSync('loginInfo').user.aliasName
    })
    wx.request({
      url: app.globalData.API + '/serverProvider/findInfo',
      data: {
        id:wx.getStorageSync('loginInfo').user.id
      },
      method: 'POST', 
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      success: function(res){
        // success
        if(res.data.code == 10000){
          var info = res.data.obj;
          if(info.loginName){
            that.setData({
              loginName:info.loginName
            })
          }
          if(info.serverProvider.name){
            that.setData({
              aliasName:info.serverProvider.name
            })
          }
          if(info.serverProvider.phoneNumber){
            that.setData({
              phoneNumber:info.serverProvider.phoneNumber
            })
          }
          if(info.image.absoluteImagePath){
            that.setData({
              loginImg:info.image.absoluteImagePath
            })
          }
        }
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    });
  },
  onReady:function(){
    // 页面渲染完成
    console.log('页面渲染完成');
  },
  onShow:function(){
    var that = this;
    // 页面显示
    console.log('页面显示');
    that.setData({
      loginImg:wx.getStorageSync('loginInfo').image.absoluteImagePath,
      loginName:wx.getStorageSync('loginInfo').user.loginName,
      aliasName:wx.getStorageSync('loginInfo').user.aliasName
    })
    wx.request({
      url: app.globalData.API + '/serverProvider/findInfo',
      data: {
        id:wx.getStorageSync('loginInfo').user.id
      },
      method: 'POST', 
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      success: function(res){
        // success
        if(res.data.code == 10000){
          var info = res.data.obj;
          if(info.loginName){
            that.setData({
              loginName:info.loginName
            })
          }
          if(info.serverProvider.name){
            that.setData({
              aliasName:info.serverProvider.name
            })
          }
          if(info.serverProvider.phoneNumber){
            that.setData({
              phoneNumber:info.serverProvider.phoneNumber
            })
          }
          if(info.image.absoluteImagePath){
            that.setData({
              loginImg:info.image.absoluteImagePath
            })
          }
        }
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    });
  },
  onHide:function(){
    // 页面隐藏
    console.log('页面隐藏');
  },
  onUnload:function(){
    // 页面关闭
    console.log('页面关闭');
  },
  goToPwdEdit:function(){
    wx.navigateTo({
      url: '../editPwd/pwd',
      success: function(res){
        // success
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  },
  exitLogin:function(){
    var that = this;
    that.setData({
      loading:true
    })
    wx.request({
      url: app.globalData.API + '/apploginout',
      data: {
        
      },
      method: 'GET', 
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      success: function(res){
        // success
        wx.navigateTo({
            url: '../index/index'
          })
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
        that.setData({
          loading:false
        })
      }
    });
  },
  imageLook:function(){
    wx.previewImage({
      current: wx.getStorageSync('loginInfo').image.absoluteImagePath, // 当前显示图片的http链接
      urls: [wx.getStorageSync('loginInfo').image.absoluteImagePath] // 需要预览的图片http链接列表
    })
  },
  editInfo:function(){
    wx.navigateTo({
      url: '../editInfo/info'
    })
  },
  onPullDownRefresh: function(){//下拉刷新
    var that = this;
    that.setData({
      loginImg:wx.getStorageSync('loginInfo').image.absoluteImagePath,
      loginName:wx.getStorageSync('loginInfo').user.loginName,
      aliasName:wx.getStorageSync('loginInfo').user.aliasName
    })
    wx.request({
      url: app.globalData.API + '/serverProvider/findInfo',
      data: {
        id:wx.getStorageSync('loginInfo').user.id
      },
      method: 'POST', 
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      success: function(res){
        // success
        if(res.data.code == 10000){
          var info = res.data.obj;
          if(info.loginName){
            that.setData({
              loginName:info.loginName
            })
          }
          if(info.serverProvider.name){
            that.setData({
              aliasName:info.serverProvider.name
            })
          }
          if(info.serverProvider.phoneNumber){
            that.setData({
              phoneNumber:info.serverProvider.phoneNumber
            })
          }
          if(info.image.absoluteImagePath){
            that.setData({
              loginImg:info.image.absoluteImagePath
            })
          }
        }
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    });
    setTimeout(function(){
            //停止刷新
            wx.stopPullDownRefresh()
          },2000)
  }
})